// pages/askForm/askForm.js
const api = require('../../utils/API/api.js')
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    date: '2016-09-01',
    leave_type_list: [],
    leave_type_list_index: 1,
    status: '0',
    place: '',
    reason: '',
    phone: '',
    error: '',
    start_time: '',
    end_time: ''
  },
  bindPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      leave_type_list_index: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    this.up_leave_type()

    const eventChannel = this.getOpenerEventChannel()
    console.log(eventChannel)
    if (util.is_dict_elemt(eventChannel)) {
         this.get_data_by_id(eventChannel)
    }

  },
  /**
   * 提交
   */
  submit_js: function (e) {
    if (!this.format_check()) return
    var datas = {}
    // 获取类型
    datas.ask_type = this.data.leave_type_list[this.data.leave_type_list_index] //this.data.leave_type_list_index// 
    // 获取时间
    datas.time_go = this.selectComponent('.start-time').data.nowTime;
    datas.time_back = this.selectComponent('.end-time').data.nowTime;

    if(datas.time_go >= datas.time_back){
      wx.showToast({
        icon:'none',
        title: '请假时间太短',
      })
      return
    }

    // 获取请假信息
    datas.place = this.data.place
    datas.reason = this.data.reason
    datas.phone = this.data.phone
    // 提交类型
    var sub = e.currentTarget.dataset.sub;

     if (sub=='1'||sub=='0'){
      datas.id = this.data.id
      datas.status = sub
      if(this.data.id){
        this.draft_put(datas)
      }else{
        this.draft_post(datas)
      }
    }else if(sub=='3'){
      datas.id = this.data.id
      datas.status = this.data.status
      this.draft_put(datas)
    }else if(sub=='2'){
      this.draft_delete({id:this.data.id})
    }
  },
  /**
   * 发送提交请求
   * @param {*} data 
   */
  draft_post: function (data) {
    api.ask.draft_post(data).then(res => {

      if(res.code==2000){
        wx.showModal({
          title: '提示',
          content: '提交成功',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        this.init_form_data()
      }

    }).catch(err => {
      wx.showModal({
        title: '提示',
        content: '提交失败',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
         
            console.log('用户点击取消')
          }
        }
      })
    })
  },
    /**
   * 发送修改请求
   * @param {*} data 
   */
  draft_put: function (data) {
    api.ask.draft_put(data).then(res => {
      wx.showModal({
        title: '提示',
        content: '提交成功',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    })
  },
  /**
   * 删除当前请假条
   * @param {*} data 
   */
  draft_delete: function (data) {
    api.ask.draft_delete(data).then(res=>{
      // this.up_leave_type()
      wx.redirectTo({
        url: '/pages/ask/ask',
      })
    })
    
  },
  /**
   * 获取请假类型
   */
  up_leave_type: function () {
    api.ask.leave_type().then(res => {
      this.setData({
        leave_type_list: res.data
      })
    })
  },
  /**
   * 初始化表单
   * @param {*} params 
   */
  init_form_data:function (params) {
    this.setData({
      place: '',
      reason: '',
      phone: '',
      leave_type_list_index: 0,
    })
  },
  /**
   * 格式检查
   */
  format_check: function (params) {
    var b1 = this.data.place == ''
    var b2 = this.data.reason == ''
    var b3 = this.data.phone == ''
    if (b1 || b2 || b3) {
      wx.showToast({
        icon:'none',
        title: '内容不能为空',
      })
      return false
    }
    return true
  },
  /**
   * 获取请假条详细信息
   * @param {*} eventChannel 
   */
  get_data_by_id: function (eventChannel) {
    var _id = 0
    eventChannel.on('data_page', function (id) {
      console.log(id)
      _id = id
    })
    this.setData({
      id:_id
    })
    if(_id==0)return
    api.ask.draft_get({ id: _id }).then(res => {
      console.log(res.data.status)
      this.setData({
        place: res.data.place,
        reason: res.data.reason,
        phone: res.data.contact_info,
        status: res.data.status,
        end_time: res.data.end_time,
        start_time: res.data.start_time,
        leave_type_list_index: res.data.ask_type
      })

    })
  }
})